/**
 * @Author: lusx
 * @Date:   14-03-2019 04:46:59
 * @Project: Govern督办
 * @Last modified by:   lusx
 * @Last modified time: 20-03-2019 03:40:20
 */
import { Toast, Tools, Modal } from "weaver-mobile";
import { Form } from 'weaver-mobile-page';
import * as Apis from "../../apis/govern";
import { observable, action, toJS } from "mobx";
import agent from "./agent.js";
import btnStore from "./btnStore.js";
const getLabel = Tools.getLabel;
const alert = Modal.alert;
const alertHandle = (params, handle,index = 1) => {
  return alert(
    btnStore.Tooltips[params.btnType][0],
    btnStore.Tooltips[params.btnType][index],
    [
      {
        text: getLabel(201, "取消"),
        onPress: () => {
          return false;
        }
      },
      {
        text: getLabel(826, "确定"),
        onPress: () => {
          agent.Btn.click(params.id, params.btnType).then(data => {
            if (handle) {
              handle();
              if(params.hasOwnProperty('handle') && params.handle !== null){
                params.handle();
              }
            } else {
              null;
            }
            return Promise.resolve();
          });
        }
      }
    ]
  );
};
const BtnTypes = {
  "2": params => {
    return agent.Btn.click(params.id, params.btnType).then(data => {
      btnStore.Toasts[params.btnType]();
    });
  },
  "3": params => {
    return agent.Btn.click(params.id, params.btnType);
  },
  "4": params => {
    return agent.Btn.click(params.id, params.btnType);
  },
  "5": params => {
    return agent.Btn.click(params.id, params.btnType);
  },
  "6": params => {
    alertHandle(params, btnStore.Toasts[params.btnType]);
    return Promise.resolve();
  },
  "7": params => {
    alertHandle(params, btnStore.Toasts[params.btnType]);
    return Promise.resolve();
  },
  "8": params => {
    return agent.Btn.click(params.id, params.btnType);
  },
  "9": params => {
    return agent.Btn.click(params.id, params.btnType).then(data => {
      btnStore.Toasts[params.btnType]();
    });
  },
  "11": params => {
    return agent.Btn.click(params.id, params.btnType);
  },
  "12": params => {
    return agent.Btn.click(params.id, params.btnType).then(data => {
      btnStore.Toasts[params.btnType]();
    });
  },
  "13": params => {
    alertHandle(params, btnStore.Toasts[params.btnType]);
    return Promise.resolve();
  },
  "17": params => {
    // alertHandle(params);
    // return Promise.resolve();
    btnStore.Toasts[params.btnType]();
    return Promise.resolve();
  },
  "18": params => {
    // alertHandle(params);
    // return Promise.resolve();
    btnStore.Toasts[params.btnType]();
    return Promise.resolve();
  },
  "1003": params => {
    // agent.Btn.click(params.id, "4").then((data)=>{
    //   if(data.conditions.length > 0){
    //     const haschildTask = 2;
    //     alertHandle(params, btnStore.Toasts[params.btnType],haschildTask);
    //   }
    // });
    const {id} = params;
    Apis.getProInfo({id:id}).then(data=>{
      if (data.status === '1') {
        const showTextIndex = data.hasChild === true ? 2 : 1;
        alertHandle(params, btnStore.Toasts[params.btnType],showTextIndex);
      }
    });
    return Promise.resolve();
  },
  "1004": params => {
    //alertHandle(params, btnStore.Toasts[params.btnType]);
    const {id} = params;
    Apis.getProInfo({id:id}).then(data=>{
      if (data.status === '1') {
        const showTextIndex = data.otherParam.superStatus != 3 ? 1 : 2;
        alertHandle(params, btnStore.Toasts[params.btnType],showTextIndex);
      }
    });
    return Promise.resolve();
  }
};
const SubTypes = {
  "3": params => {
    return agent.SubBtn.click(params.id, params.btnType, params.v).then(() => {
      btnStore.Toasts[params.btnType]();
      return true;
    });
  },
  "4": params => {
    const newv = [
      {
        id: "0",
        status: "0",
        changed: "0",
        data: { ...params.v },
        index: "0"
      }
    ];
    return agent.SubBtn.click(params.id, params.btnType, newv).then(() => {
      btnStore.Toasts[params.btnType]();
      return true;
    });
  },
  "5": params => {
    return agent.SubBtn.click(params.id, params.btnType, params.v).then(() => {
      btnStore.Toasts[params.btnType]();
      return true;
    });
  },
  "8": params => {
    return agent.SubBtn.click(params.id, params.btnType, params.v).then(() => {
      btnStore.Toasts[params.btnType]();
      return true;
    });
  },
  "11": params => {
    return agent.SubBtn.click(params.id, params.btnType, params.v).then((data) => {
      btnStore.Toasts[params.btnType]();
      return {id:params.id};
      // Apis.getShareList({ id: params.id }).then(data => {
      //   btnStore.Toasts[params.btnType]();
      // });
    });
  }
};
export default {
  BtnTypes,
  SubTypes
};
